import StringUtil from "./string_util";

class Color {
  bit24String = null;
  bit24Updated = !1;
  constructor(e?) {
    let t = this;
    if (arguments.length >= 1) {
      let n = e.replace(/^#|^0x/, ""),
        r = parseInt(n, 16);
      if ((n.length != 6 && n.length != 8) || isNaN(r))
        throw new Error("Invalid color value: " + e);
      let i, s, o, u;
      n.length == 6
        ? ((i = (r >> 16) & 255),
          (s = (r >> 8) & 255),
          (o = r & 255),
          (u = 255))
        : n.length == 8 &&
          ((i = (r >> 24) & 255),
          (s = (r >> 16) & 255),
          (o = (r >> 8) & 255),
          (u = r & 255)),
        t.r(i),
        t.g(s),
        t.b(o),
        t.a(u);
    }
  }
  static colorWithString(e) {
    return new Color(e);
  }
  static colorWithRGBA(e, n, r, i) {
    let s = new Color();
    return s.r(e), s.g(n), s.b(r), arguments.length < 4 && (i = 255), s.a(i), s;
  }
  static randomColor(e, n) {
    (e = isNaN(e) ? 0 : e), (n = isNaN(n) ? 16777215 : n);
    let r = (e >> 16) & 255,
      i = (e >> 8) & 255,
      s = e & 255,
      o = (n >> 16) & 255,
      u = (n >> 8) & 255,
      a = n & 255,
      f = r + Math.floor(Math.random() * (o - r)),
      l = r + Math.floor(Math.random() * (u - i)),
      c = r + Math.floor(Math.random() * (a - s));
    return Color.colorWithRGBA(f, l, c, 255);
  }
  to24BitString(t) {
    let n = this;
    if (n.bit24String == null || n.bit24Updated === !1) {
      let r = StringUtil.fillStringTo(n.r().toString(16), "0", 2).toUpperCase(),
        i = StringUtil.fillStringTo(n.g().toString(16), "0", 2).toUpperCase(),
        s = StringUtil.fillStringTo(n.b().toString(16), "0", 2).toUpperCase();
      (t = arguments.length >= 1 ? t : "#"),
        (n.bit24String = t + r + i + s),
        (n.bit24Updated = !0);
    }
    return n.bit24String;
  }
  bit32String = null;
  bit32Updated = !1;
  to32BitString(t) {
    let n = this;
    if (n.bit32String == null || n.bit32Updated === !1) {
      let r = StringUtil.fillStringTo(n.r().toString(16), "0", 2).toUpperCase(),
        i = StringUtil.fillStringTo(n.g().toString(16), "0", 2).toUpperCase(),
        s = StringUtil.fillStringTo(n.b().toString(16), "0", 2).toUpperCase(),
        o = StringUtil.fillStringTo(n.a().toString(16), "0", 2).toUpperCase();
      (t = arguments.length >= 1 ? t : "#"),
        (n.bit32String = t + r + i + s + o),
        (n.bit32Updated = !0);
    }
    return n.bit32String;
  }
  rgbString = null;
  rgbStringUpdated = !1;
  toRGBString() {
    let e = this;
    if (e.rgbString == null || e.rgbStringUpdated === !1) {
      e.rgbStringUpdated = !0;
      let t = Math.round(e.r()),
        n = Math.round(e.g()),
        r = Math.round(e.b());
      e.rgbString =
        "rgb(" +
        t.toString(10) +
        "," +
        n.toString(10) +
        "," +
        r.toString() +
        ")";
    }
    return e.rgbString;
  }
  rgbaString = null;
  rgbaStringUpdated = !1;
  toRGBAString() {
    let e = this;
    if (e.rgbaString == null || e.rgbaStringUpdated === !1) {
      e.rgbaStringUpdated = !0;
      let t = Math.round(e.r()),
        n = Math.round(e.g()),
        r = Math.round(e.b()),
        i = Math.round((e.a() / 255) * 100) / 100;
      e.rgbaString =
        "rgba(" +
        t.toString(10) +
        "," +
        n.toString(10) +
        "," +
        r.toString() +
        "," +
        i.toString() +
        ")";
    }
    return e.rgbaString;
  }
  _r = 0;
  r(e?) {
    let t = this;
    return (
      arguments.length > 0 &&
        e != t._r &&
        ((t._r = Math.round(e)),
        (t.bit24Updated = !1),
        (t.bit32Updated = !1),
        (t.rgbaStringUpdated = !1),
        (t.rgbStringUpdated = !1)),
      t._r
    );
  }
  _g = 0;
  g(e?) {
    let t = this;
    return (
      arguments.length > 0 &&
        e != t._g &&
        ((t._g = Math.round(e)),
        (t.bit24Updated = !1),
        (t.bit32Updated = !1),
        (t.rgbaStringUpdated = !1),
        (t.rgbStringUpdated = !1)),
      t._g
    );
  }
  _b = 0;
  b(e?) {
    let t = this;
    return (
      arguments.length > 0 &&
        e != t._b &&
        ((t._b = Math.round(e)),
        (t.bit24Updated = !1),
        (t.bit32Updated = !1),
        (t.rgbaStringUpdated = !1),
        (t.rgbStringUpdated = !1)),
      t._b
    );
  }
  _a = 0;
  a(e?) {
    let t = this;
    return (
      arguments.length > 0 &&
        e != t._a &&
        ((t._a = Math.round(e)),
        (t.bit24Updated = !1),
        (t.bit32Updated = !1),
        (t.rgbaStringUpdated = !1),
        (t.rgbStringUpdated = !1)),
      t._a
    );
  }
  adjustSaturation(e) {
    let t = this,
      n = (t.r() + t.g() + t.b()) / 3,
      r = n + (t.r() - n) * e,
      i = n + (t.g() - n) * e,
      s = n + (t.b() - n) * e;
    t.r(Math.floor(r)),
      t.g(Math.floor(i)),
      t.b(Math.floor(s)),
      (t.bit24Updated = !1),
      (t.bit32Updated = !1),
      (t.rgbaStringUpdated = !1),
      (t.rgbStringUpdated = !1);
  }
  equalsTo(e) {
    let t = this;
    return (
      t.r() === e.r() && t.g() === e.g() && t.b() === e.b() && t.a() === e.a()
    );
  }
  clone() {
    let e = this;
    return Color.colorWithRGBA(e.r(), e.g(), e.b(), e.a());
  }
}

export default Color;
